package com.gm.commonlib.network.basenetwork;


import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import io.reactivex.Observable;
import retrofit2.Retrofit;
public abstract class HttpServicesHolder<T1, T2> {
    private Class<T1> mClazz;

    public HttpServicesHolder() {
        initT1Class();
    }

    public Observable<T2> getHttpObservable() {
        T1 instance = createRetrofit().create(mClazz);
        return getHttpObservable(instance);
    }

    protected Retrofit createRetrofit() {
        return BaseHttpClient.getHttpclient().getRetrofitInstanse();
    }

    public abstract Observable<T2> getHttpObservable(T1 instance);

    private void initT1Class() {
        Class clazz = getClass();
        while (clazz != Object.class) {
            Type t = clazz.getGenericSuperclass();
            if (t instanceof ParameterizedType) {
                Type[] args = ((ParameterizedType) t).getActualTypeArguments();
                if (args != null && args.length >= 1 && args[0] instanceof Class) {
                    this.mClazz = (Class<T1>) args[0];
                    break;
                }
            }
            clazz = clazz.getSuperclass();
        }
    }
}
